



//
//  NGovHomeViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/19.
//  Copyright © 2018年 youedata. All rights reserved.
#import "NNGAnalysisViewController.h"
#import "NMineCell.h"
#import "NGovHomeViewController.h"
#import "NGovHomeCell.h"
#import "NGovHomeHeader.h"
#import "NBusiServeAnalisisViewController.h"
#import "NIndustryDataAnalysisViewController.h"
#import "NRoleSelectedViewController.h"
#import "NProfileModifyViewController.h"
#import "NHistroyRecordViewController.h"
#import "NMessageViewController.h"
@interface NGovHomeViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NGovHomeHeader *header;

@property (nonatomic,strong)NSArray *picArray;

@property (nonatomic,strong)NSArray *titles;

@property (nonatomic,copy)NSString *headImgUrl;

@end

#define cellIdentifier @"mineid"
#define ImageHeight 200

@implementation NGovHomeViewController


-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-49) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if ([NUserModel shareInstance].isLogin) {
        if ([[NUserModel shareInstance].badgeCount integerValue]>0) {
            [self.navigationItem.rightBarButtonItem pp_addDotWithColor:[UIColor redColor]];
        }
    }else{
        [self.navigationItem.rightBarButtonItem pp_addDotWithColor:nil];
    }
    ///如果没有登录就直接调用登录
    if ([NUserModel shareInstance].isLogin) {
        
        self.picArray = @[@"",@"transfer",@"mine5"];
        self.titles = @[[NUserModel shareInstance].userInfoName,@"切换用户角色",@"统计分析"];
        
    }else{
        
//        [self callLoginVC];
        self.picArray = @[@"headicon",@"transfer",@"mine5"];
        self.titles = @[@"请登录",@"切换用户角色",@"统计分析"];
        return;
    }
    
    if ([NUserModel shareInstance].isLogin) {
        [self loadUserData];
    }
    [self.tableView reloadData];
}

- (void)loadUserData{
    
    //
    WeakStament(ws);
    
    NSDictionary *dic = @{@"userInfoId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@""};
    
    [[NNRCNetRequest manager]getWithUrl:kUserInfoUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        
        if ([result[@"code"] integerValue]==0) {
            self.headImgUrl = result[@"page"][@"userInfoUrl"];
//            self.nickName = result[@"page"][@"userInfoNickname"];
//            self.userInfoName = result[@"page"][@"userInfoName"];
            [ws.tableView reloadData];
        }
    } failure:^(id failure) {
        
        YYLog(@"%@",failure);
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"政府主页" textColor:kNavigationTitleColor fontSize:18 itemType:center];
     [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"消息中心" selectedIMG:@"消息中心" tartget:self action:@selector(messageAction)];
    
    
    //    NGovHomeHeader *header = [[[NSBundle mainBundle] loadNibNamed:@"NGovHomeHeader" owner:self options:nil] lastObject];
    //    header.frame = CGRectMake(0, 0, ScreenWidth, 40);
    //    self.tableView.tableFooterView = header;
    
    //    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
    //    img.image = WDImgName(@"firstPageBanner1");
    //    self.tableView.tableFooterView = img;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushAction) name:kPushNoti object:nil];
    [self.view addSubview:self.tableView];
    
//    if ([NUserModel shareInstance].isLogin) {
//        if ([[NUserModel shareInstance].badgeCount integerValue]>0) {
//
//            [self.navigationItem.rightBarButtonItem pp_addDotWithColor:[UIColor redColor]];
//        }
//    }
//    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
///消息中心
- (void)messageAction {
    YYLog(@"跳往消息中心");
    NMessageViewController *msg = [NMessageViewController new];
    msg.userType = [NUserModel shareInstance].userInfoType;
    msg.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:msg animated:YES];
    
}

- (void)pushAction {
    NHistroyRecordViewController *record = [NHistroyRecordViewController new];
    record.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:record animated:YES];
}

///登录测试
- (void)loadData{
    //userInfoType  1企业  2政府
    NSDictionary *dic = @{@"userInfoType":@"2",@"userInfoName":@"admin",@"userInfoNamePwd":@"111111"};
    
    [[NNRCNetRequest manager]getWithUrl:kLoginUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        
        if ([result[@"code"] isEqualToString:@"0"]) {
            //存储用户id
            [NUserModel shareInstance].userInfoId = result[@"page"][@"userInfoId"];
        }
    } failure:^(id failure) {
         YYLog(@"%@",failure);
    }];
   
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
        {
            if ([NUserModel shareInstance].isLogin) {
                
                NProfileModifyViewController *modify = [NProfileModifyViewController new];
                modify.userName = [NUserModel shareInstance].userInfoName;
                modify.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:modify animated:YES];
                
            }else{
                
                [self callLoginVC];
            }
        }
            break;
            
            
        case 1:
        {
            
            //切换用户角色
            
            //            [self callRoleSelecteVC];
            
           
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"确认重新选择用户角色? \n 已登录帐号将退出"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确认",nil];
            [alert show];
        }
            break;
            
        case 2:
        {
            
        }
            break;
            
        default:
            break;
    }
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        
        YYLog(@"点击取消");
        
    }else{
        [self callTransferVC];
    }
}

- (void)callTransferVC{
    
    NRoleSelectedViewController *role = [[NRoleSelectedViewController alloc] initWithNibName:@"NRoleSelectedViewController" bundle:nil];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:role];
    //解决present延迟弹出
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    });
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 15)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section==2) {
        UIView *view = [UIView new];
        view.frame = CGRectMake(0, 0, ScreenWidth, 120);
        view.backgroundColor = [UIColor magentaColor];
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ImageHeight)];
        img.image = WDImgName(@"firstPageBanner1");
        [view addSubview:img];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        view.userInteractionEnabled = YES;
        [view addGestureRecognizer:tap];
        
        return view;
    }
    
    return nil;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{
    
    return 15.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section==2) {
        return ImageHeight;
    }else{
        return 0.f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        return 90.f;
        
    }else{
        return 55.f;
    }
}



#pragma mark - datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NMineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell configWithPicName:self.picArray[indexPath.section] title:self.titles[indexPath.section] withIndexPath:indexPath];
    
    if (indexPath.section==0) {
        
//        cell.setLabel.hidden = YES;
        
        if ([NUserModel shareInstance].isLogin) {
            [cell.LogoImage sd_setImageWithURL:[NSURL URLWithString:self.headImgUrl] placeholderImage:WDImgName(@"headicon")];
        }else{
            cell.LogoImage.image = WDImgName(@"headicon");
        }
        
    }
    
    if (indexPath.section==2) {
        cell.arrowImg.hidden = YES;
    }
    
    return cell;
}


//营商统计分析
- (void)tapAction{
    
    if ([NUserModel shareInstance].isLogin) {
        
        NNGAnalysisViewController *analysis = [NNGAnalysisViewController new];
        analysis.month = 10;
        analysis.webUrl = @"";
        analysis.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:analysis animated:YES];
//        NBusiServeAnalisisViewController *busi = [NBusiServeAnalisisViewController new];
//        busi.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:busi animated:YES];
    }else{
        [self callLoginVC];
    }
    
}
@end
